﻿using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;

namespace HobSoft.SoccerBet2014.Common.Wpf.Converter
{
    public class DataGridCellColorConverter : IMultiValueConverter, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var cell = value as DataGridCell;
            if (cell == null)
            {
                return Default;
            }

            var column = cell.Column.DisplayIndex.ToString();
            if (ColumnBrush.ContainsKey(column))
            {
                return ColumnBrush[column];
            }
            return Default;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        public Dictionary<string, Brush> ColumnBrush { get; set; }
        public Dictionary<string, Brush> RowBrush { get; set; }
        public Brush Default { get; set; }

        public DataGridCellColorConverter()
        {
            RowBrush = new Dictionary<string, Brush>();
            ColumnBrush = new Dictionary<string, Brush>();
            Default = new SolidColorBrush(Colors.Transparent);
        }
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (values.Length < 2)
                return Default;
            var cell = values[0] as DataGridCell;
            var row = values[1] as DataGridRow;
            if (cell == null || row == null)
            {
                return Default;
            }
            var column = cell.Column.DisplayIndex.ToString();
            if (ColumnBrush.ContainsKey(column))
            {
                return ColumnBrush[column];
            }
            var rowKey = row.AlternationIndex.ToString();
            if (RowBrush.ContainsKey(rowKey))
            {
                return RowBrush[rowKey];
            }
            return Default;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
